<script>
import {
  delProduct_receipt,
  getProduct_receipt, storeProduct_receipt
} from '@/api/yyj/wm/inbound/product_receipt'

export default {
  dicts: ['gbs_good_type'],
  data() {
    return {
      // 表单参数
      form: {},
    }
  },
  computed: {
    getGbsGoodType() {
      return this.dict.type.gbs_good_type
    },
    productTypeName() {
      // 查找与 form.prGoodType 值对应的产品类型名字
      const type = this.getGbsGoodType.find(dict => dict.value === this.form.prGoodType);
      return type ? type.label : ''; // 如果找到，则返回名字，否则返回空字符串
    }
  },
  created() {
    this.reset();
    this.getData();
  },
  methods: {
    // 表单重置
    reset() {
      this.form = {
        prCode: null,
        prName: null,
        prReceiptDate: null,
        prOrderCode: null,
        prOrderName: null,
        prDesc: '',
        prProductCode: null,
        prBatch: null,
        prReceiptNum: null,
        prProductionDate: null,
        prExpirationDate: null,
        prStoLocation: null,
        prStatus: null,
        prProductName: null,
        prProductSpecType: null,
        prGoodType: null,
        prProductUnitName: null,
        prWName: null,
        wManager: null,
        prSaName: null,
        prSlName: null,
      };
      this.resetForm("form");
    },
    // 查询入库单详情
    getData() {
      const prCode = this.$route.query.prCode;
      getProduct_receipt(prCode).then(response => {
        this.form = response.data;
        this.form.wManager = response.data.wmanager;
      })
    },
    // 页面退出事件
    handlePageClose() {
      this.$tab.closeOpenPage();
      this.$tab.refreshPage({
        path: '/wm/inbound/product_receipt'
      });
    },
    // 编辑按钮
    handleEdit() {
      const prCode = this.form.prCode;
      this.$router.push({
        path: '/warehouse/inbound/product_receipt/edit',
        query: {
          prCode: prCode
        }
      })
    },
    // 删除按钮
    handleDelete() {
      const prCode = this.form.prCode;
      this.$modal.confirm('是否确认删除产品入库单编号为"' + prCode + '"的数据项？').then(function() {
        return delProduct_receipt(prCode);
      }).then(() => {
        this.$modal.msgSuccess("删除成功");
        this.handlePageClose();
      }).catch(() => {});
    },
    // 入库按钮
    handleStorage() {
      const prCode = this.form.prCode;
      const prStatus = 2;

      const prStoLocation = this.form.prStoLocation;
      const prProductCode = this.form.prProductCode;
      const prReceiptNum = this.form.prReceiptNum;
      const prBatch = this.form.prBatch;
      const prProductionDate = this.form.prProductionDate;
      const prExpirationDate = this.form.prExpirationDate;

      const prOrderCode = this.form.prOrderCode;

      this.$modal.confirm('是否确认入库产品入库单编号为"' + prCode + '"的数据项？').then(function() {
        return storeProduct_receipt({
          prCode,
          prStatus,
          prStoLocation,
          prProductCode,
          prReceiptNum,
          prBatch,
          prProductionDate,
          prExpirationDate,
          prOrderCode,
        });
      }).then(() => {
        this.$modal.msgSuccess("入库成功");
        this.handlePageClose();
      }).catch(() => {});
    },
    // 撤回按钮
    handleRecall() {
      const prCode = this.form.prCode;
      const prStatus = 1;

      const prProductCode = this.form.prProductCode;
      const prReceiptNum = this.form.prReceiptNum;
      const prBatch = this.form.prBatch;

      const prOrderCode = this.form.prOrderCode;

      this.$modal.confirm('是否确认撤回产品入库单编号为"' + prCode + '"的数据项？').then(function() {
        return storeProduct_receipt({
          prCode,
          prStatus,
          prProductCode,
          prReceiptNum,
          prBatch,
          prOrderCode,
        })
      }).then(() => {
        this.$modal.msgSuccess("撤回成功");
        this.handlePageClose();
      }).catch(() => {});
    }
  }
}
</script>

<template>
  <div class="app-container">
    <div class="page-body">
      <div v-if="form.prStatus === '1'">
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-edit"
              size="mini"
              @click="handleEdit"
              v-hasPermi="['wm:inbound:product_receipt:edit']"
            >编辑</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              @click="handleDelete"
              v-hasPermi="['wm:inbound:product_receipt:remove']"
            >删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-upload"
              size="mini"
              @click="handleStorage"
              v-hasPermi="['wm:inbound:product_receipt:store']"
            >入库</el-button>
          </el-col>
        </el-row>
      </div>
      <div v-if="form.prStatus === '2'">
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-undo"
              size="mini"
              @click="handleRecall"
              v-hasPermi="['wm:inbound:product_receipt:recall']"
            >撤回</el-button>
          </el-col>
        </el-row>
      </div>

      <el-divider>
        <div class="my-divider">基础信息</div>
      </el-divider>

      <el-form ref="form" :model="form" size="small" :inline="true">
        <el-row class="mb8">
          <el-col :span="10" :offset="2">
            <el-form-item label="入库单名称" prop="prName" label-width="92px">
              {{this.form.prName}}
            </el-form-item>
          </el-col>
          <el-col :span="10" :offset="2">
            <el-form-item label="入库日期" prop="prReceiptDate" label-width="92px">
              {{this.form.prReceiptDate}}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row class="mb8">
          <el-col :span="10" :offset="2">
            <el-form-item label="工单名称" prop="prOrderName" label-width="92px">
              {{this.form.prOrderName}}
            </el-form-item>
          </el-col>
          <el-col :span="10" :offset="2">
            <el-form-item label="工单编号" prop="prOrderCode" label-width="92px">
              {{this.form.prOrderCode}}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row class="mb8">
          <el-col :span="24" :offset="2">
            <el-form-item label="备注" prop="prDesc" label-width="92px">
              {{this.form.prDesc}}
            </el-form-item>
          </el-col>
        </el-row>

        <el-divider>
          <div class="my-divider">入库明细</div>
        </el-divider>

        <el-row class="mb8">
          <el-col :span="10" :offset="2">
            <el-form-item label="产品名称" prop="prProductName" label-width="92px">
              {{this.form.prProductName}}
            </el-form-item>
          </el-col>
          <el-col :span="10" :offset="2">
            <el-form-item label="产品编号" prop="prProductCode" label-width="92px">
              {{this.form.prProductCode}}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row class="mb8">
          <el-col :span="10" :offset="2">
            <el-form-item label="规格型号" prop="prProductSpecType" label-width="92px">
              {{this.form.prProductSpecType}}
            </el-form-item>
          </el-col>
          <el-col :span="10" :offset="2">
            <el-form-item label="产品类型" prop="prGoodType" label-width="92px">
              {{productTypeName}}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row class="mb8">
          <el-col :span="10" :offset="2">
            <el-form-item label="单位" prop="prProductUnitName" label-width="92px">
              {{this.form.prProductUnitName}}
            </el-form-item>
          </el-col>
          <el-col :span="10" :offset="2">
            <el-form-item label="批次号" prop="prBatch" label-width="92px">
              {{this.form.prBatch}}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row class="mb8">
          <el-col :span="10" :offset="2">
            <el-form-item label="入库数量" prop="prReceiptNum" label-width="92px">
              {{this.form.prReceiptNum}}
            </el-form-item>
          </el-col>
          <el-col :span="10" :offset="2">
            <el-form-item label="生产日期" prop="prProductionDate" label-width="92px">
              {{this.form.prProductionDate}}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row class="mb8">
          <el-col :span="10" :offset="2">
            <el-form-item label="到期日期" prop="prExpirationDate" label-width="92px">
              {{this.form.prExpirationDate}}
            </el-form-item>
          </el-col>
          <el-col :span="10" :offset="2">
            <el-form-item label="仓库名称" prop="prWName" label-width="92px">
              {{this.form.prWName}} / {{ this.form.prSaName }} / {{ this.form.prSlName }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row class="mb8">
          <el-col :span="10" :offset="2">
            <el-form-item label="仓库负责人" prop="wManager" label-width="92px">
              {{this.form.wManager}}
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <el-divider />

      <el-row class="mb8">
        <el-col :span="24" style="text-align: center">
          <el-button @click="handlePageClose">关闭</el-button>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<style scoped lang="scss">
.page-body {
  width: 1000px;
  margin: 0 auto;
}

.formInput,.formSelect {
  width: 220px;
}

.formTextarea {
  width: 721px;
}
</style>
